﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using GameView.Comment;
using GameView.Services;
using GameView.Services.Comment;
using GameView.Services.Dtos.RechargesDto;
using GameView.Views;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace GameView.ViewModels
{
    public partial class FastRechargeViewModel : ObservableObject, IRecipient<RechargeRecodeMsg>, IRecipient<NotificationMsg>
    {
        private readonly RechargeService _service;
        private readonly IMessenger _messenger;

        public FastRechargeViewModel(RechargeService service, IMessenger messenger)
        {
            _service = service;
            var view = (UserControl)ExtentHelper.GetService<RechargeAddNewView>();
            RechargeAddNewViewModel vm = (RechargeAddNewViewModel)view.DataContext;
            vm.IsDialog = false;
            CurrentView = view;
            _messenger = messenger;
            _messenger.RegisterAll(this);
            IsMerchant = StaticValues.LoginInfo.PersonInfo.RoleId != 3;
            LoadFromApi().ConfigureAwait(false);
        }

        [ObservableProperty]
        private RechargeQueryDto _queryDto = new();

        [ObservableProperty]
        private bool _isLoading = false;

        [ObservableProperty]
        private object _currentView;

        [ObservableProperty]
        private ObservableCollection<RechargeRecord> _rechargeRecords = [];

        [ObservableProperty]
        private bool _needLoading = false;

        [ObservableProperty]
        private bool _isMerchant = false;

        [RelayCommand]
        private void SelectionChanged(RechargeRecord item)
        {
            if (item != null)
                _messenger.Send(new RechargeRecodeMsg(item, "fList"));
        }

        public void Receive(RechargeRecodeMsg message)
        {
            if (message.Source != "add")
                return;
            if (RechargeRecords.Count > 50)
                RechargeRecords.RemoveAt(50);
            RechargeRecords.Insert(0, message.Value);
        }

        private async Task LoadFromApi()
        {
            var res = await _service.GetTop(50, true);
            if (res.StatusCode != 200)
            {
                GlobalMessage.Instance.ShowError(JSON.Serialize(res.Errors));
                return;
            }
            RechargeRecords.Clear();
            foreach (var item in res.Data)
            {
                RechargeRecords.Add(item);
            }
        }

        public void Receive(NotificationMsg message)
        {
            if (message == null || message.Value == null || message.Value.Type <= 0)
                return;
            if (message.Value.Type != 1201)
                return;
            if (!message.Value.Content.Contains('|'))
                return;
            var sp = message.Value.Content.Split('|');
            var order = sp[0];
            message.Value.Content = sp[1];
            var code = RechargeRecords.FirstOrDefault(q => q.OrderNumber == order);
            if (code == null)
                return;
            int statu = 3002;
            if (message.Value.Content.EndsWith("成功！"))
            {
                statu = 3001;
            }
            else if (message.Value.Content.EndsWith("失败！"))
            {
                statu = 3002;
            }
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                code.RechargeOrderStatus = statu;
            });
        }
    }
}